package com.lerning.edu.manage.controller;

import com.lerning.edu.commons.util.DateUtil;
import com.lerning.edu.commons.util.StringUtil;
import com.lerning.edu.services.CampusDSRService;
import com.lerning.edu.services.JMSODSKPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangwei
 * @since 18/10/25
 */
@Controller
@RequestMapping("jmsODS")
public class JMSODSKPIController {

    @Autowired
    private JMSODSKPIService jmsodskpiService;

    @Autowired
    private CampusDSRService campusDSRService;

    @RequestMapping("/kpi")
    public String kpi(HttpServletRequest request,
                      @RequestParam(value = "AutoID", required = false) String AutoID) {

        Map dateMap = new HashMap();
        List months = campusDSRService.queryFinanceMonths();

        if (StringUtil.isEmpty(AutoID)) {
            List FinanceMonthOfDq = campusDSRService.queryFinanceMonthOfDq(DateUtil.dayMap());//当前月

            dateMap = (HashMap) FinanceMonthOfDq.get(0);
            AutoID = dateMap.get("AutoID").toString();
        } else {
            dateMap = campusDSRService.queryFinanceMonth(Integer.parseInt(AutoID));//财务月开始和结束时间
        }

        dateMap.put("Text", dateMap.get("EndDate").toString().substring(0,7));

        List KPIByCampus = jmsodskpiService.queryKPIByCampus(dateMap);

        request.setAttribute("FinanceMonths", months);
        request.setAttribute("AutoID", AutoID);
        request.setAttribute("KPIByCampus", KPIByCampus);
        request.setAttribute("listSize", KPIByCampus.size());

        return "JMS/KpiOfCampus";
    }

    @RequestMapping("/saveUpdate")
    public String saveUpdate(HttpServletRequest request,
                             @RequestParam(value = "AutoID", required = false) String AutoID,
                             @RequestParam(value = "cID", required = false) String cID,
                             @RequestParam(value = "reNewKpi", required = false) String reNewKpi,
                             @RequestParam(value = "refKpi", required = false) String refKpi,
                             @RequestParam(value = "mktKpi", required = false) String mktKpi){
        Map dateMap = campusDSRService.queryFinanceMonth(Integer.parseInt(AutoID));//财务月开始和结束时间

        String[] ids = cID.split(",");
        String[] renNews = reNewKpi.split(",");
        String[] refs = refKpi.split(",");
        String[] mkts = mktKpi.split(",");

        List renNewList = new ArrayList();
        List refList = new ArrayList();
        List mktList = new ArrayList();

        for (int i = 0; i < ids.length; i++) {

            Map reNewMap = new HashMap();
            reNewMap.put("cID", ids[i]);
            reNewMap.put("reNewKpiValue", renNews[i]);
            reNewMap.put("refKpiValue", refs[i]);
            reNewMap.put("mktKpiValue", mkts[i]);

            renNewList.add(reNewMap);
            refList.add(reNewMap);
            mktList.add(reNewMap);
        }

        Map map = new HashMap();
        map.put("AutoID",AutoID);

        map.put("reNewKpi", renNewList);
        map.put("refKpi", refList);
        map.put("mktKpi", mktList);

        map.put("Text", dateMap.get("EndDate").toString().substring(0,7));

        jmsodskpiService.updateAdd(map);

        return "redirect:/jmsODS/kpi?AutoID=" + AutoID ;
    }

    @RequestMapping("/kpiByCC")
    public String kpi(HttpServletRequest request,
                      @RequestParam(value = "AutoID", required = false) String AutoID,
                      @RequestParam(value = "campusID", required = false)String campusID) {

        Map dateMap = new HashMap();
        List months = campusDSRService.queryFinanceMonths();

        if (StringUtil.isEmpty(AutoID)) {
            List FinanceMonthOfDq = campusDSRService.queryFinanceMonthOfDq(DateUtil.dayMap());//当前月

            dateMap = (HashMap) FinanceMonthOfDq.get(0);
            AutoID = dateMap.get("AutoID").toString();
        } else {
            dateMap = campusDSRService.queryFinanceMonth(Integer.parseInt(AutoID));//财务月开始和结束时间
        }

        dateMap.put("Text", dateMap.get("EndDate").toString().substring(0,7));
        dateMap.put("campusID", '|'+campusID+'|');

        List KPIByCC = jmsodskpiService.queryKPIByCC(dateMap);

        request.setAttribute("FinanceMonths", months);
        request.setAttribute("AutoID", AutoID);
        request.setAttribute("KPIByCC", KPIByCC);
        request.setAttribute("listSize", KPIByCC.size());

        request.setAttribute("campusID", campusID);

        return "JMS/KpiOfCC";
    }

    @RequestMapping("/saveUpdateByCC")
    public String saveUpdateByCC(HttpServletRequest request,
                             @RequestParam(value = "campusID", required = false)String campusID,
                             @RequestParam(value = "AutoID", required = false) String AutoID,
                             @RequestParam(value = "cID", required = false) String cID,
                             @RequestParam(value = "cName", required = false) String cName,
                             @RequestParam(value = "reNewKpi", required = false) String reNewKpi,
                             @RequestParam(value = "refKpi", required = false) String refKpi,
                             @RequestParam(value = "mktKpi", required = false) String mktKpi){
        Map dateMap = campusDSRService.queryFinanceMonth(Integer.parseInt(AutoID));//财务月开始和结束时间

        String[] ids = cID.split(",");
        String[] cNames = cName.split(",");
        String[] renNews = reNewKpi.split(",");
        String[] refs = refKpi.split(",");
        String[] mkts = mktKpi.split(",");

        List renNewList = new ArrayList();
        List refList = new ArrayList();
        List mktList = new ArrayList();

        for (int i = 0; i < ids.length; i++) {

            Map reNewMap = new HashMap();

            reNewMap.put("cID", ids[i].replace("|", ""));
            reNewMap.put("cName", cNames[i]);
            reNewMap.put("reNewKpiValue", renNews[i]);
            reNewMap.put("refKpiValue", refs[i]);
            reNewMap.put("mktKpiValue", mkts[i]);

            renNewList.add(reNewMap);
            refList.add(reNewMap);
            mktList.add(reNewMap);
        }

        Map map = new HashMap();
        map.put("AutoID",AutoID );

        map.put("reNewKpi", renNewList);
        map.put("refKpi", refList);
        map.put("mktKpi", mktList);

        map.put("Text", dateMap.get("EndDate").toString().substring(0,7));

        jmsodskpiService.updateAddByCC(map);

        return "redirect:/jmsODS/kpiByCC?AutoID=" + AutoID + "&campusID=" + campusID;
    }
}
